home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / util / libs / graphics3d.lha / src / library / graphics3Df_i2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-17  |  8.9 KB  |  332 lines

  1. /*
  2. **      $VER: graphics3Df_i2.c 10.01 (01.11.97)
  3. **
  4. **      Internal functions for graphics3D.library
  5. **
  6. **      (C) Copyright 97 Patrizio Biancalani
  7. **      All Rights Reserved.
  8. **
  9. **    Note: this code is traslate from the blitzbasic 3d graphics engine 
  10. **          V 0.9 of Maciej R. Gorny.
  11. */
  12.  
  13. #include <exec/types.h>
  14. #include <exec/memory.h>
  15. #include <proto/exec.h>
  16. #include <proto/intuition.h>
  17. #include <intuition/intuition.h>
  18. #include <intuition/screens.h>
  19.  
  20. #include <graphics/rastport.h>
  21. #include <graphics/clip.h>
  22. #include <graphics/regions.h>
  23. #include <graphics/gfx.h>
  24. #include <graphics/gfxmacros.h>
  25. #include <graphics/layers.h>
  26.  
  27. #include "graphics3Dc.h"
  28. #include "graphics3D.h"
  29.  
  30. /**** prototipi locali ****/
  31. long int abs(val);
  32.  
  33. /************** routin interne solo matematiche *************/
  34.  
  35. /**************************************/
  36. /** crea una matrice 4x4 d'identita' **/
  37. /**************************************/
  38. void matidentity4x4(imatrix)
  39. struct matrix4x4 *imatrix;
  40. {
  41. imatrix->r0c0=FIXV; imatrix->r0c1=0; imatrix->r0c2=0; imatrix->r0c3=0;
  42. imatrix->r1c0=0; imatrix->r1c1=FIXV; imatrix->r1c2=0; imatrix->r1c3=0;
  43. imatrix->r2c0=0; imatrix->r2c1=0; imatrix->r2c2=FIXV; imatrix->r2c3=0;
  44. imatrix->r3c0=0; imatrix->r3c1=0; imatrix->r3c2=0; imatrix->r3c3=FIXV;
  45. }
  46.  
  47. /****************************/
  48. /** azzera una matrice 4x4 **/
  49. /****************************/
  50. void matzero4x4(imatrix)
  51. struct matrix4x4 *imatrix;
  52. {
  53. imatrix->r0c0=0; imatrix->r0c1=0; imatrix->r0c2=0; imatrix->r0c3=0;
  54. imatrix->r1c0=0; imatrix->r1c1=0; imatrix->r1c2=0; imatrix->r1c3=0;
  55. imatrix->r2c0=0; imatrix->r2c1=0; imatrix->r2c2=0; imatrix->r2c3=0;
  56. imatrix->r3c0=0; imatrix->r3c1=0; imatrix->r3c2=0; imatrix->r3c3=0;
  57. }
  58.  
  59. /***************************************/
  60. /** copia una matrice 4x4 in un'altra **/
  61. /***************************************/
  62. void matcopy4x4(s_m,d_m)
  63. struct matrix4x4 *s_m;
  64. struct matrix4x4 *d_m;
  65. {
  66. d_m->r0c0=s_m->r0c0; d_m->r0c1=s_m->r0c1; d_m->r0c2=s_m->r0c2;
  67. d_m->r0c3=s_m->r0c3;
  68. d_m->r1c0=s_m->r1c0; d_m->r1c1=s_m->r1c1; d_m->r1c2=s_m->r1c2;
  69. d_m->r1c3=s_m->r1c3;
  70. d_m->r2c0=s_m->r2c0; d_m->r2c1=s_m->r2c1; d_m->r2c2=s_m->r2c2;
  71. d_m->r2c3=s_m->r2c3;
  72. d_m->r3c0=s_m->r3c0; d_m->r3c1=s_m->r3c1; d_m->r3c2=s_m->r3c2;
  73. d_m->r3c3=s_m->r3c3;
  74. }
  75.  
  76. /*********************************************/
  77. /** calcola il prodotto tra due matrici 4x4 **/
  78. /*********************************************/
  79. void matmult4x4(a,b,r)
  80. struct matrix4x4 *a;
  81. struct matrix4x4 *b;
  82. struct matrix4x4 *r;
  83. {
  84. r->r0c0=a->r0c0*b->r0c0+a->r0c1*b->r1c0+a->r0c2*b->r2c0+a->r0c3*b->r3c0;
  85. r->r0c0=r->r0c0 >> SFIXV;
  86. r->r0c1=a->r0c0*b->r0c1+a->r0c1*b->r1c1+a->r0c2*b->r2c1+a->r0c3*b->r3c1;
  87. r->r0c1=r->r0c1 >> SFIXV;
  88. r->r0c2=a->r0c0*b->r0c2+a->r0c1*b->r1c2+a->r0c2*b->r2c2+a->r0c3*b->r3c2;
  89. r->r0c2=r->r0c2 >> SFIXV;
  90. r->r0c3=a->r0c0*b->r0c3+a->r0c1*b->r1c3+a->r0c2*b->r2c3+a->r0c3*b->r3c3;
  91. r->r0c3=r->r0c3 >> SFIXV;
  92.  
  93. r->r1c0=a->r1c0*b->r0c0+a->r1c1*b->r1c0+a->r1c2*b->r2c0+a->r1c3*b->r3c0;
  94. r->r1c0=r->r1c0 >> SFIXV;
  95. r->r1c1=a->r1c0*b->r0c1+a->r1c1*b->r1c1+a->r1c2*b->r2c1+a->r1c3*b->r3c1;
  96. r->r1c1=r->r1c1 >> SFIXV;
  97. r->r1c2=a->r1c0*b->r0c2+a->r1c1*b->r1c2+a->r1c2*b->r2c2+a->r1c3*b->r3c2;
  98. r->r1c2=r->r1c2 >> SFIXV;
  99. r->r1c3=a->r1c0*b->r0c3+a->r1c1*b->r1c3+a->r1c2*b->r2c3+a->r1c3*b->r3c3;
  100. r->r1c3=r->r1c3 >> SFIXV;
  101.  
  102. r->r2c0=a->r2c0*b->r0c0+a->r2c1*b->r1c0+a->r2c2*b->r2c0+a->r2c3*b->r3c0;
  103. r->r2c0=r->r2c0 >> SFIXV;
  104. r->r2c1=a->r2c0*b->r0c1+a->r2c1*b->r1c1+a->r2c2*b->r2c1+a->r2c3*b->r3c1;
  105. r->r2c1=r->r2c1 >> SFIXV;
  106. r->r2c2=a->r2c0*b->r0c2+a->r2c1*b->r1c2+a->r2c2*b->r2c2+a->r2c3*b->r3c2;
  107. r->r2c2=r->r2c2 >> SFIXV;
  108. r->r2c3=a->r2c0*b->r0c3+a->r2c1*b->r1c3+a->r2c2*b->r2c3+a->r2c3*b->r3c3;
  109. r->r2c3=r->r2c3 >> SFIXV;
  110.  
  111. r->r3c0=a->r3c0*b->r0c0+a->r3c1*b->r1c0+a->r3c2*b->r2c0+a->r3c3*b->r3c0;
  112. r->r3c0=r->r3c0 >> SFIXV;
  113. r->r3c1=a->r3c0*b->r0c1+a->r3c1*b->r1c1+a->r3c2*b->r2c1+a->r3c3*b->r3c1;
  114. r->r3c1=r->r3c1 >> SFIXV;
  115. r->r3c2=a->r3c0*b->r0c2+a->r3c1*b->r1c2+a->r3c2*b->r2c2+a->r3c3*b->r3c2;
  116. r->r3c2=r->r3c2 >> SFIXV;
  117. r->r3c3=a->r3c0*b->r0c3+a->r3c1*b->r1c3+a->r3c2*b->r2c3+a->r3c3*b->r3c3;
  118. r->r3c3=r->r3c3 >> SFIXV;
  119. }
  120.  
  121. /*********************************************/
  122. /** calcola il prodotto tra due matrici 4x4 **/
  123. /** ottimizzato per matrici di rotazione    **/
  124. /*********************************************/
  125. void matmult4x4s(a,b,r)
  126. struct matrix4x4 *a;
  127. struct matrix4x4 *b;
  128. struct matrix4x4 *r;
  129. {
  130.  
  131. r->r0c0=a->r0c0*b->r0c0+a->r0c1*b->r1c0+a->r0c2*b->r2c0;
  132. r->r0c0=r->r0c0 >> SFIXV;
  133. r->r0c1=a->r0c0*b->r0c1+a->r0c1*b->r1c1+a->r0c2*b->r2c1;
  134. r->r0c1=r->r0c1 >> SFIXV;
  135. r->r0c2=a->r0c0*b->r0c2+a->r0c1*b->r1c2+a->r0c2*b->r2c2;
  136. r->r0c2=r->r0c2 >> SFIXV;
  137. r->r0c3=0;
  138.  
  139. r->r1c0=a->r1c0*b->r0c0+a->r1c1*b->r1c0+a->r1c2*b->r2c0;
  140. r->r1c0=r->r1c0 >> SFIXV;
  141. r->r1c1=a->r1c0*b->r0c1+a->r1c1*b->r1c1+a->r1c2*b->r2c1;
  142. r->r1c1=r->r1c1 >> SFIXV;
  143. r->r1c2=a->r1c0*b->r0c2+a->r1c1*b->r1c2+a->r1c2*b->r2c2;
  144. r->r1c2=r->r1c2 >> SFIXV;
  145. r->r1c3=0;
  146.  
  147. r->r2c0=a->r2c0*b->r0c0+a->r2c1*b->r1c0+a->r2c2*b->r2c0;
  148. r->r2c0=r->r2c0 >> SFIXV;
  149. r->r2c1=a->r2c0*b->r0c1+a->r2c1*b->r1c1+a->r2c2*b->r2c1;
  150. r->r2c1=r->r2c1 >> SFIXV;
  151. r->r2c2=a->r2c0*b->r0c2+a->r2c1*b->r1c2+a->r2c2*b->r2c2;
  152. r->r2c2=r->r2c2 >> SFIXV;
  153. r->r2c3=0;
  154.  
  155. r->r3c0=a->r3c0*b->r0c0+a->r3c1*b->r1c0+a->r3c2*b->r2c0;
  156. r->r3c0=r->r3c0 >> SFIXV;
  157. r->r3c1=a->r3c0*b->r0c1+a->r3c1*b->r1c1+a->r3c2*b->r2c1;
  158. r->r3c1=r->r3c1 >> SFIXV;
  159. r->r3c2=a->r3c0*b->r0c2+a->r3c1*b->r1c2+a->r3c2*b->r2c2;
  160. r->r3c2=r->r3c2 >> SFIXV;
  161. r->r3c3=0;
  162. }
  163.  
  164. /****************************************************/
  165. /** calcola prodotto tra una matrice 1x4 e una 4x4 **/
  166. /** risultato in una matrice 1x4           **/
  167. /****************************************************/
  168. void matmult1x4s(a,b,r)
  169. struct matrix1x4 *a;
  170. struct matrix4x4 *b;
  171. struct matrix1x4 *r;
  172. {
  173. r->r0c0=a->r0c0*b->r0c0+a->r0c1*b->r0c1+a->r0c2*b->r0c2+a->r0c3*b->r0c3;
  174. r->r0c0=r->r0c0 >> SFIXV;
  175. r->r0c1=a->r0c0*b->r1c0+a->r0c1*b->r1c1+a->r0c2*b->r1c2+a->r0c3*b->r1c3;
  176. r->r0c1=r->r0c1 >> SFIXV;
  177. r->r0c2=a->r0c0*b->r2c0+a->r0c1*b->r2c1+a->r0c2*b->r2c2+a->r0c3*b->r2c3;
  178. r->r0c2=r->r0c2 >> SFIXV;
  179. r->r0c3=a->r0c0*b->r3c0+a->r0c1*b->r3c0+a->r0c2*b->r3c2+a->r0c3*b->r3c3;
  180. r->r0c3=r->r0c3 >> SFIXV;
  181. }
  182.  
  183. /************************************************/
  184. /** crea un vettore da 2 punti nello spazio 3d **/
  185. /************************************************/
  186. void makevector3d(a,b,result)
  187. struct vertex *a;
  188. struct vertex *b;
  189. struct vector *result;
  190. {
  191.  
  192. result->x=b->x - a->x;
  193. result->y=b->y - a->y;
  194. result->z=b->z - a->z;
  195.  
  196. }
  197.  
  198. /**************************************/
  199. /** ritorna l'ampiezza di un vettore **/
  200. /** nota:                 **/
  201. /** siccome posso trattare al mass.  **/
  202. /** numeri a 32bit elimino la        **/
  203. /** parte frazionaria, tanto l'errore**/
  204. /** introdotto e' minimo in tal caso.**/
  205. /** Il  risultato e' in FIXPOINT.    **/
  206. /**************************************/
  207. long int vectormag3d(a)
  208. struct vector *a;
  209. {
  210. long int p,x,y,z;
  211.  
  212. x=(a->x+FIXVM) >> SFIXV;
  213. y=(a->y+FIXVM) >> SFIXV;
  214. z=(a->z+FIXVM) >> SFIXV;
  215. p=x*x+y*y+z*z;
  216. p=sqri(p);
  217.  
  218. return (p);
  219. }
  220.  
  221. /************************************/
  222. /* calcolo vettore normale ad un    */
  223. /* poligono.                */
  224. /* E' ottimizzato.            */
  225. /* le componenti del vettore sono   */
  226. /* espresse come numeri in FIXPOINT.*/
  227. /************************************/
  228. void normalpol(v0,v1,v2,normal)
  229. struct vector *normal;
  230. struct vertex *v2,*v1,*v0;
  231. {
  232. long int x0,y0,z0;
  233. long int x,y,z;
  234. long int x1,y1,z1;
  235.  
  236. x0=v0->x + FIXVM;
  237. y0=v0->y + FIXVM;
  238. z0=v0->z + FIXVM;
  239.  
  240. x=(v1->x - x0) >> SFIXV;
  241. y=(v1->y - y0) >> SFIXV;
  242. z=(v1->z - z0) >> SFIXV;
  243.  
  244. x1=(v2->x - x0) >> SFIXV;
  245. y1=(v2->y - y0) >> SFIXV;
  246. z1=(v2->z - z0) >> SFIXV;
  247.  
  248. normal->x=(y*z1-z*y1) << SFIXV;
  249. normal->y=(-x*z1+z*x1) << SFIXV;
  250. normal->z=(x*y1-y*x1) << SFIXV;
  251.  
  252. }
  253.  
  254. /*****************************************/
  255. /** ritorna il prodotto tra due vettori **/
  256. /** risultato in fixpoint            **/
  257. /** nota:                **/
  258. /** siccome posso trattare al mass.     **/
  259. /** numeri a 32bit                      **/
  260. /** risultato non in fix point ma int.  **/
  261. /*****************************************/
  262. long int dotproduct(u,v)
  263. struct vector *v;
  264. struct vector *u;
  265. {
  266. long int x,y,z;
  267. long int x1,y1,z1;
  268.  
  269. x=(u->x + FIXVM) >> SFIXV ;
  270. y=(u->y + FIXVM) >> SFIXV ;
  271. z=(u->z + FIXVM) >> SFIXV ;
  272.  
  273. x1=(v->x + FIXVM) >> SFIXV ;
  274. y1=(v->y + FIXVM) >> SFIXV ;
  275. z1=(v->z + FIXVM) >> SFIXV ;
  276.  
  277. return(x*x1+y*y1+z*z1);
  278. }
  279.  
  280. /************************************/
  281. /** calcolo radice quadrata di un  **/
  282. /** numero intero.           **/
  283. /** risultato in fixpoint          **/
  284. /************************************/
  285. long int sqri(v)
  286. long int v;
  287. {
  288. long int p,v1,d,r,f;
  289.  
  290. v1=v;
  291.  
  292. f=(v1>32000L)?1:0;
  293.  
  294. if (f==NULL) v1=v << (SFIXV+SFIXV);
  295.  
  296. /** uso metodo delle approssimazioni successive pero' a passo variabile **/
  297. /* prima pero' calcolo una base il piu' vicina possibile alla radice */
  298. d=v1>>1;
  299. p=0;
  300. if (d>45000L) d=20000L;
  301. while (d>NULL)
  302.     {
  303.     r=p+d;
  304.     if (r*r>v1) 
  305.         {
  306.         d=d>>1;
  307.         }
  308.         else
  309.         {
  310.         p=r;
  311.         }
  312.     }
  313.  
  314. if (f!=NULL) p=p << SFIXV;
  315.  
  316. return(p);
  317. }
  318.  
  319. /************************************
  320.  ** calcolo valore assoluto di un  **
  321.  ** intero fixpoint o meno.       **
  322.  ************************************/
  323. long int abs(val)
  324. long int val;
  325. {
  326. if (val>=0) return(val);
  327.  
  328. return(-val);
  329. }
  330.  
  331. /********************* fine *******************************/
  332.